package com.qidian.by.goods.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.qidian.by.goods.config.FanoutProperties;
import com.qidian.by.goods.service.IItemService;
import com.qidian.by.goods.service.RabbitTemplateClientService;
import com.qidian.by.goods.vo.ItemVO;
import com.qidian.by.util.pojo.EsResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: CoolPro
 * @Description:
 * @Time: 2024-05-17 19:30
 **/

public class RabbitTemplateClientServiceImpl implements RabbitTemplateClientService {
    private String router;

    public RabbitTemplateClientServiceImpl(String router) {
        this.router = router;
    }

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private Properties fanoutExchangeProperty;

    @Override
    public void send(String msg) {
//        先读取消息拿到商品ID
        JSONObject itemInf = EsResult.json2object(msg, JSONObject.class);
        //发送消息
        //注册一个确认回调
        if(!rabbitTemplate.isConfirmListener())
            rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
                if (!ack) {
                    //消息发送失败了
                    //如果消息发送失败了就存到Redis中去
                    stringRedisTemplate
                            .opsForHash()
                            .put(
                                    fanoutExchangeProperty.getProperty("item-message-confirm-tag"),
                                    itemInf.get("option")+ "_" + itemInf.get("itemId"),
                                    EsResult.object2json(Map.of("op", fanoutExchangeProperty.getProperty("op-insert"),
                                            "itemId", itemInf.get("itemId"),
                                            "cause", cause))
                            );
                }
            });

        //注册一个失败回调
        if(!rabbitTemplate.isReturnListener())
            rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
                stringRedisTemplate
                        .opsForHash()
                        .put(
                                fanoutExchangeProperty.getProperty("item-message-fall-tag"),
                                itemInf.get("option") + "_" + itemInf.get("itemId"),
                                EsResult.object2json(Map.of("op", fanoutExchangeProperty.getProperty("op-insert"),
                                        "message", message,
                                        "replyCode", replyCode,
                                        "exchange", exchange,
                                        "routingKey", routingKey,
                                        "itemId", itemInf.get("itemId"),
                                        "replyText", replyText)
                                )
                        );
            });
        rabbitTemplate.convertAndSend(fanoutExchangeProperty.getProperty("fanout-exchange"),router,msg);
    }
}
